package com.fnzx.service.impl;

import com.fnzx.dao.PersonalUserDao;
import com.fnzx.entity.CooperativePersonalRelationEntity;
import com.fnzx.entity.CooperativeUserEntity;
import com.fnzx.entity.PersonalUserEntity;
import com.fnzx.entity.vo.UserInfoVo;
import com.fnzx.service.CooperativePersonalRelationService;
import com.fnzx.service.PersonalUserService;
import com.fnzx.service.UserService;
import com.fnzx.util.PageUtils;
import com.fnzx.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;



@Service("personalUserService")
public class PersonalUserServiceImpl extends ServiceImpl<PersonalUserDao, PersonalUserEntity> implements PersonalUserService {
    @Autowired
    UserService userService;

    @Autowired
    CooperativePersonalRelationService relationService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PersonalUserEntity> page = this.page(
                new Query<PersonalUserEntity>().getPage(params),
                new QueryWrapper<PersonalUserEntity>()
        );
        List<PersonalUserEntity> records = this.list().stream().filter(item -> {
            Long personalId = item.getId();
            CooperativePersonalRelationEntity relationEntity = relationService.getOne(new QueryWrapper<CooperativePersonalRelationEntity>().eq("personal_id", personalId));
            if (relationEntity != null) {
                //过滤掉赋能中心的个人顾问
                return relationEntity.getCooperativeId() != 1;
            }
            return true;
        }).collect(Collectors.toList());

        List<UserInfoVo> collect = records.stream()
                .map(record -> userService.getUserInfoById(record.getId()))
                .collect(Collectors.toList());
        collect.removeAll(Collections.singleton(null));

        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(collect);
        return pageUtils;
    }

}